package cn.edu.qzuie.artist.module.td.service.participateactivitiy;

import org.springframework.stereotype.Service;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.participateactivitiy.ParticipateActivitiyDO;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;

import cn.edu.qzuie.artist.module.td.dal.mysql.participateactivitiy.ParticipateActivitiyMapper;

import javax.annotation.Resource;

import static cn.edu.qzuie.artist.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.edu.qzuie.artist.module.td.enums.ErrorCodeConstants.*;

/**
 * 参加活动 Service 实现类
 *
 * @author 泉信管理员
 */
@Service
@Validated
public class ParticipateActivitiyServiceImpl implements ParticipateActivitiyService {

    @Resource
    private ParticipateActivitiyMapper participateActivitiyMapper;

    @Override
    public Long createParticipateActivitiy(ParticipateActivitiySaveReqVO createReqVO) {
        // 插入
        ParticipateActivitiyDO participateActivitiy = BeanUtils.toBean(createReqVO, ParticipateActivitiyDO.class);
        participateActivitiyMapper.insert(participateActivitiy);
        // 返回
        return participateActivitiy.getId();
    }

    @Override
    public void updateParticipateActivitiy(ParticipateActivitiySaveReqVO updateReqVO) {
        // 校验存在
        validateParticipateActivitiyExists(updateReqVO.getId());
        // 更新
        ParticipateActivitiyDO updateObj = BeanUtils.toBean(updateReqVO, ParticipateActivitiyDO.class);
        participateActivitiyMapper.updateById(updateObj);
    }

    @Override
    public void deleteParticipateActivitiy(Long id) {
        // 校验存在
        validateParticipateActivitiyExists(id);
        // 删除
        participateActivitiyMapper.deleteById(id);
    }

    private void validateParticipateActivitiyExists(Long id) {
        if (participateActivitiyMapper.selectById(id) == null) {
            throw exception(PARTICIPATE_ACTIVITIY_NOT_EXISTS);
        }
    }

    @Override
    public ParticipateActivitiyDO getParticipateActivitiy(Long id) {
        return participateActivitiyMapper.selectById(id);
    }

    @Override
    public PageResult<ParticipateActivitiyDO> getParticipateActivitiyPage(ParticipateActivitiyPageReqVO pageReqVO) {


        return participateActivitiyMapper.selectPage(pageReqVO);
    }

    /**
     * 多表
     * @param pageReqVO
     * @return
     */
    @Override
    public PageResult<ParticipateActivitiyPagesRespVO> getParticipateActivitiyPageWithName(ParticipateActivitiyPageReqVO pageReqVO) {
        ParticipateActivitiyPageReqVO reqVO = new ParticipateActivitiyPageReqVO();
        BeanUtils.copyProperties(pageReqVO,reqVO);
        return participateActivitiyMapper.selectPageWithName(pageReqVO,reqVO);
    }

}